﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Net.Mail;
using System.Security.Cryptography;
using System.Text;
using System.Text.RegularExpressions;
using Image = System.Drawing.Image;
using Size = System.Drawing.Size;

namespace Project5188.BUS
{
    public static class Extensions
    {
        public enum PageNames
        {
            ParameterPage = 1,
            ParameterValuePage = 2,
            UserPage = 3,
            BranchPage = 4,
            CommunicationInfoPage = 5,
            DinnerExpenseDetailPage = 6,
            DressInfoPage = 7,
            IdCardInfoPage = 8,
            InstitutionalPage = 9,
            InsuranceInfoPage = 10,
            PersonnelActionPage = 11,
            PersonnelInfoPage = 12,
            RegionPage = 13,
            StudentInfoPage = 14,
            AdminInfoReportPage = 15,
            PersonnelNomineePage = 16,
            PersonnelWorkOutPage = 17,
            CustomerDigitalInfoReportPage = 18,
            CustomerSalesInfoReportPage = 19,
            CreateOfferPage = 20,
            CustomerInfoPage = 21,
            CustomerSalesInfoPage = 22,
            InspectionInfoPage = 23,
            InstitutionInspectionPage = 24,
            PersonnelInspectionPage = 25,
            CoursePage = 26,
            RollCallPage = 27,
            StudentRegistryPage = 28,
            TeacherRegistryPage = 29,
            TimeTablePage = 30,
            SuccessStatusPage = 31,
            DocumentPage = 32,
            DocumentFlowPage = 33
        } ;

        public enum Modules
        {
            SystemManagement = 1,
            Operations = 2,
            Personnel = 3,
            SalesAndCustomerRelations = 4,
            Inspection = 5,
            EducationAndTraining = 6,
            DocumentManagement = 7
        };

        public enum ProcessTypes
        {
            Select = 1,
            Insert = 2,
            Update = 3,
            Delete = 4
        } ;

        public static IList<T> Clone<T>(this IList<T> listToClone) where T : ICloneable
        {
            return listToClone.Select(item => (T)item.Clone()).ToList();
        }

        public static object ChangeType(object value, Type conversionType)
        {
            if (conversionType == null)
            {
                throw new ArgumentNullException("conversionType");
            } // end if

            if (conversionType.IsGenericType &&
              conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>)))
            {
                if (value == null || (value is string && (string) value == String.Empty))
                {
                    return null;
                } // end if

                var nullableConverter = new NullableConverter(conversionType);
                conversionType = nullableConverter.UnderlyingType;
            } // end if

            return Convert.ChangeType(value, conversionType);
        }

        public static Size GetThumbnailSize(Image original)
        {
            // Maximum size of any dimension.
            const int maxPixels = 150;

            // Width and height.
            int originalWidth = original.Width;
            int originalHeight = original.Height;

            // Compute best factor to scale entire image based on larger dimension.
            double factor;
            if (originalWidth > originalHeight)
            {
                factor = (double)maxPixels / originalWidth;
            }
            else
            {
                factor = (double)maxPixels / originalHeight;
            }

            // Return thumbnail size.
            return new Size((int)(originalWidth * factor), (int)(originalHeight * factor));
        }

        public static string Md5Hashing(string value)
        {
            var x = new MD5CryptoServiceProvider();
            var data = Encoding.ASCII.GetBytes(value);
            data = x.ComputeHash(data);
            var ret = String.Empty;
            for (var i = 0; i < data.Length; i++)
                ret += data[i].ToString("x2").ToLower();
            return ret;
        }

        public static string SendMail(string sendTo, string sendSubject, string sendMessage)
        {
            try
            {
                var bTest = ValidateEmailAddress(sendTo);

                // if the email address is bad, return message
                if (bTest == false)
                    throw new Exception("Invalid recipient email address: " + sendTo);

                var paramValue = new ParameterValues();

                var sendFrom = paramValue.GetEntityByParameterId(39)[0].Value;

                // create the email message
                var message = new MailMessage(
                    sendFrom,
                    sendTo,
                    sendSubject,
                    sendMessage);

                var smtpUserInfo = new System.Net.NetworkCredential(paramValue.GetEntityByParameterId(37)[0].Value, paramValue.GetEntityByParameterId(38)[0].Value);

                // create smtp client at mail server location
                var client = new SmtpClient(paramValue.GetEntityByParameterId(33)[0].Value) { UseDefaultCredentials = false, Credentials = smtpUserInfo, EnableSsl = true };

                // send message
                client.Send(message);

                return "Message sent to " + sendTo + " at " + DateTime.Now + ".";
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message);
            }
        }

        public static bool ValidateEmailAddress(string emailAddress)
        {
            var textToValidate = emailAddress;
            var expression = new Regex(@"\w+@[a-zA-Z_]+?\.[a-zA-Z]{2,3}");

            return expression.IsMatch(textToValidate);
        }
    }
}
